package com.lxm.ymcc.mq.consumer;
import com.alibaba.fastjson.JSON;
import com.lxm.ymcc.constans.Mq;
import com.lxm.ymcc.domain.PayOrder;
import com.lxm.ymcc.dto.MqOrder;
import com.lxm.ymcc.service.IPayOrderService;
import com.lxm.ymcc.util.AssertUtil;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@RocketMQMessageListener(topic = Mq.ORDER_TOPIC,
        selectorExpression=Mq.ORDER_TAGS	//tags
        ,consumerGroup = "service-pay-consumer"
        ,messageModel = MessageModel.CLUSTERING ) //消息的消费模式 CLUSTERING：集群    BROADCASTING：广播
public class CourserPayConsumer implements RocketMQListener<MessageExt> {


    @Autowired
    private IPayOrderService payOrderService;
    @Override
    public void onMessage(MessageExt message) {
        byte[] body = message.getBody();
        String s = new String(body);
        // 数据还原
        MqOrder mqOrder = JSON.parseObject(s, MqOrder.class);
        // 防止订单重复 查询订单号
        PayOrder payOrder = payOrderService.selectOneSn(mqOrder.getOrderNo());
        AssertUtil.isNull(payOrder,"订单已经创建");

        // 保存支付订单
        payOrder = new PayOrder();
        BeanUtils.copyProperties(mqOrder, payOrder);
        payOrder.setCreateTime(new Date());
        payOrder.setPayStatus(0);
        payOrderService.insert(payOrder);
    }
}
